<?php
namespace app\index\controller;

use app\common\controller\IndexBase;
use think\Db;
use think\Session;

class Depot extends IndexBase
{
	public function index(){
		$userinfo = Session::get('userInfo');
		$data = Db::name('depot')->where('user_id',$userinfo['id'])->select();
		$goods = Db::name('goods')->where('is_default',1)->find();
		foreach ($data as $k => $v) {
			if($v['type'] == 2)
				$goods['price'] = $goods['price'] * $v['rebate'];
			$data[$k]['num'] = floor($v['money']/$goods['price']);
		}
		$this->assign('data',$data);
		return $this->fetch();
	}

	public function take(){
		$userinfo = Session::get('userInfo');
		$address_id = DB::name('user')->where('id',$userinfo['id'])->value('address_id');
		$id = input('param.id');
		$data = Db::name('depot')->where('id',$id)->find();
		$goods = Db::name('goods')->where('is_default',1)->find();
		if($data['type'] == 2)
			$goods['price'] = $goods['price'] * $data['rebate'];
		$data['num'] = floor($data['money']/$goods['price']);
		$address = Db::name('address')->where('id',$address_id)->find();

		$this->assign('address',$address);
		$this->assign('goods',$goods);
		$this->assign('data',$data);
		return $this->fetch();
	}

	public function dzlist(){
		$userinfo = Session::get('userInfo');
		$address = Db::name('address')->where('user_id',$userinfo['id'])->order('time desc')->select();
		$this->assign('address',$address);
		return $this->fetch();
	}

	public function address(){
		if( request()->isPost() ){
			$userinfo = Session::get('userInfo');
			$data = input('post.');
			$Arr = array(
				'user_id' 	=> 	$userinfo['id'],
				'name'		=>	$data['user'],
				'phone'		=>	$data['phone'],
				'zip_code'	=>	$data['youbian'],
				'address'	=>	$data['province'].','.$data['city'].','.$data['area'].','.$data['address'],
				'status'	=>	1,
				'time'		=>	time()

			);
			$id = Db::name('address')->insertGetId($Arr);
			Db::name('user')->where('id',$userinfo['id'])->update(['address_id'=>$id]);
			if($id){
				$arr = [
                    'result' => 0,
                    'msg' => '添加成功',
                ];
			}else{
				$arr = [
                    'result' =>10004,
                    'msg' => '添加失败',
                ];
			}
			$this->json_return($arr);
		}else{
			
		}
		return $this->fetch();
	}

	public function is_dizhi(){
		if( request()->isAjax() ){
			$userinfo = Session::get('userInfo');
			$data = input('post.');
			//print_r($data);exit;
			$is = Db::name('user')->where('id',$userinfo['id'])->update(['address_id'=>$data['id']]);
			if($is){
				$arr = [
                    'result' => 0,
                    'msg' => '修改成功',
                ];
			}else{
				$arr = [
                    'result' =>10004,
                    'msg' => '修改失败',
                ];
			}
			$this->json_return($arr);
		}
	}


	//提货订单
    public function ti_order(){
        if( request()->isPost() ){
            $userinfo = Session::get('userInfo');
            $map = input('post.');
            $goods = Db::name('goods')->where('id',$map['goods_id'])->find();
            $address = Db::name("address")->where('id',$map["address_id"])->find();
            $depot = Db::name('depot')->where('id',$map['id'])->where('user_id',$userinfo['id'])->find();
            
            if(empty($depot)){
            	$this->error("订单错误");exit;
            }
            if($depot['type']==1){
            	$amount = number_format($map['goods_num']*$goods['price'],2);
            }else{
            	$amount = number_format($map['goods_num']*$goods['price']*$depot['rebate'],2);
            }
            if($amount > $depot['money']){
            	$this->error("订单错误");exit;
            }

            $Arr = array(
            	'order_sn'	=>	generateOrderCode("th",$this->userInfo["id"]),
            	'user_id'	=>	$userinfo['id'],
            	'status'	=>	1,
            	'type'		=>	$depot['type']==1?1:2,
            	'depot_id'	=>	$depot['id'],
            	'goods_id'	=>	$goods['id'],
            	'goods_name'     => $goods['name'],
                'goods_image'    => $goods['images'],
                'goods_price'    => $goods['price'],
            	'consignee'	=>	$address['name'],
            	'phone'		=>	$address['phone'],
            	'address'	=>	$address['address'],
            	'num'		=>	$map['goods_num'],
            	'amount'	=>	$amount,
            	'add_time'	=>	time()
            );
            Db::name("up_goods")->insert($Arr,false,true);
            $this->assign('goods',$goods);
            $this->assign('depot',$depot);
            $this->assign('order',$Arr);
            return $this->fetch();
        }else{
            $this->error("订单错误");
        }
    }

    public function pay(){
    	if( request()->isAjax() ){
    		$data = input('post.');
    		$userinfo = Session::get('userInfo');
    		$user = Db::name('user')->where('id',$userinfo['id'])->field('pay_pwd,pay_code')->find();
    		if(getMd5Pas($data['pwd'],$user['pay_code']) == $user['pay_pwd']){
    			updata_yc_je($data['order_id']);
    			$url = '/index/Upgoods/index';
    			$arr = [
                    'result' => 0,
                    'msg' => '支付成功',
                    'url' => $url,
                ];
    		}else{
    			$arr = [
                    'result' =>10004,
                    'msg' => '支付密码不正确',
                ];
    		}
    		$this->json_return($arr);
    	}
    	$data = input('param.sn');
    	$order_id = Db::name('up_goods')->where('order_sn',$data)->value('id');
    	$this->assign('order_id',$order_id);
    	return $this->fetch();
    }
}